package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.services.SaleService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * ClassName:SaleController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/19
 * ♣♣♣  打折商品
 */
@Controller
@RequestMapping("/sale")
public class SaleController {
    @Autowired
    private SaleService saleService;

    @MyLog("查看活动商品")
    @RequiresPermissions("promotion:sale:list")
    @RequestMapping(value = "/toSale", method = RequestMethod.GET)
    public String toSale(HttpSession session, HttpServletRequest request) {
        request.setAttribute("ddId", "Sale");
        return "saleMain";
    }

    @RequiresPermissions("promotion:sale:list")
    @RequestMapping(value = "saleWaresInfoSearch", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getSaleInfo(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String waresInfo = request.getParameter("waresInfo");
        String promotionInfo = request.getParameter("promotionInfo");
        String promotionType = request.getParameter("promotionType");
        List<Map<String, Object>> list = saleService.selectSaleWaresInfo(waresInfo, promotionInfo, promotionType);
        return PageUnit.Page(list, page, limit);
    }

    @MyLog("删除活动商品")
    @RequiresPermissions("promotion:sale:delete")
    @RequestMapping(value = "/delSaleWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteSaleWares(HttpServletRequest request) {
        String promotionId = request.getParameter("promotionId");
        String waresId = request.getParameter("waresId");
        return saleService.delSaleWares(promotionId, waresId);
    }
}
